package com.ruoyi.goods.GoodsStore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.goods.GoodsStore.domain.GoodsCategory;
import com.ruoyi.goods.GoodsStore.mapper.GoodsCategoryMapper;
import com.ruoyi.goods.GoodsStore.service.IGoodsCategoryService;
import com.ruoyi.goods.GoodsStore.service.IGoodsStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物品分类信息Service业务层处理
 *
 * @author yl
 * @date 2022-11-27
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {

    @Autowired
    private IGoodsStoreService goodsStoreService;

    /**
     * 查询物品分类信息列表
     *
     * @param goodsCategory 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory) {
        return getBaseMapper().selectGoodsCategoryList(goodsCategory);
    }

    @Override
    public List<GoodsCategory> treeCategoryDate() {
        return getBaseMapper().selectTree();
    }

    @Override
    public GoodsCategory categoryById(Long id) {
        return getBaseMapper().categoryById(id);
    }

    @Override
    public boolean checkParentAndChild(GoodsCategory goodsCategory) {
        List<GoodsCategory> list = getBaseMapper().selectAllChildren(goodsCategory.getId());
        List<Long> ids = list.stream().map(GoodsCategory::getId).collect(Collectors.toList());
        for (Long id : ids) {
            if (goodsCategory.getParentId() == id) {
                return true;//父id=其子类的id
            }
        }
        return false;
    }

    @Override
    public void deleteById(Long id) {
        //收集传入分类的id和其子类的id
        List<GoodsCategory> list = getBaseMapper().selectAllChildren(id);
        List<Long> ids = list.stream().map(GoodsCategory::getId).collect(Collectors.toList());
        ids.add(id);
        //判断有无物品未移除
        for (Long i : ids) {
            List<Integer> num = goodsStoreService.selectCategory(i);
            if (num != null && num.size() != 0) {
                throw new ServiceException("当前分类或其子类中还有未物品,请移除后再删除");
            }
        }
        getBaseMapper().deleteById(id);


    }

    @Override
    public void updateByCategory(GoodsCategory goodsCategory) {
        getBaseMapper().updateByCategory(goodsCategory);
    }

    @Override
    public List<Long> AllChildren(Long id) {
        List<GoodsCategory> goodsCategories = getBaseMapper().selectAllChildren(id);
        List<Long> ids = goodsCategories.stream().map(GoodsCategory::getId).collect(Collectors.toList());
        ids.add(id);
        return ids;

    }

    @Override
    public void removeCategory(Map<String, Object> map) {
        Long id1 = Long.valueOf(map.get("id1") + "");
        Long id2 = Long.valueOf(map.get("id2") + "");
        boolean agree = Boolean.valueOf(map.get("agree") + "");
        GoodsCategory g1 = getBaseMapper().selectById(id1);
        //子类跟随父类移动
        if (agree) {
            g1.setParentId(id2);
            getBaseMapper().updateByCategory(g1);
        } else {
            //子类不跟随父类移动
            //子类的父类没有父类,子类的父类改为空
            List<GoodsCategory> children = getBaseMapper().selectChildren(id1);
            if (g1.getParentId() == null && g1.getParentId() == 0) {
                for (GoodsCategory g : children) {
                    g.setParentId(null);
                    getBaseMapper().updateByCategory(g);
                }
                g1.setParentId(id2);
                getBaseMapper().updateByCategory(g1);
            } else {
                //子类的父类有父类,子类的父类改为父类的父类
                Long parentId = g1.getParentId();
                for (GoodsCategory g : children) {
                    g.setParentId(parentId);
                    getBaseMapper().updateByCategory(g);
                }
                g1.setParentId(id2);
                getBaseMapper().updateByCategory(g1);
            }
        }
    }
}
